﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace Frogism.Data {
  public class FriendlyReader {
    private SqlDataReader DataReader { get; set; }
    public static FriendlyReader Create(SqlDataReader reader) {
      return new FriendlyReader(reader);
    }
    private FriendlyReader(SqlDataReader reader) {
      this.DataReader = reader;
    }
    public bool Read() {
      return DataReader.Read();
    }
    public int Int(string columnName) {
      try {
        return (int)DataReader[columnName];
      } catch(Exception ex) {
        throw new FrogismDataException(columnName, ex.Message);
      }
    }
    public void DecimalSetIfNotNull(ref decimal value, string columnName)
    {
        if (!IsNull(columnName)) value = Decimal(columnName);
    }
    public void IntSetIfNotNull(ref int value, string columnName)
    {
        if (!IsNull(columnName)) value = Int(columnName);
    }
    public double Double(string columnName) {
      try {
        return (double)DataReader[columnName];
      } catch (Exception ex) {
        throw new FrogismDataException(columnName, ex.Message);
      }
    }
    public decimal Decimal(string columnName) {
      try {
        return (decimal)DataReader[columnName];
      } catch(Exception ex) {
        throw new FrogismDataException(columnName, ex.Message);
      }
    }
    public string String(string columnName) {
      try {
        return (string)DataReader[columnName];
      } catch(Exception ex) {
        throw new FrogismDataException(columnName, ex.Message);
      }
    }
    public bool Bool(string columnName) {
      try {
        return (bool)DataReader[columnName];
      } catch(Exception ex) {
        throw new FrogismDataException(columnName, ex.Message);
      }
    }
    public bool IsNull(string columnName) {
      
      int ordinal = DataReader.GetOrdinal(columnName);

      return DataReader.IsDBNull(ordinal);
    }
    public DateTime Date(string columnName) {
      try {
        return (DateTime)DataReader[columnName];
      } catch(Exception ex) {
        throw new FrogismDataException(columnName, ex.Message);
      }
    }
  }
}